//
//  MusicViewController.m
//  SnailPost
//
//  Created by 抠泥ser on 16/3/7.
//  Copyright © 2016年 抠泥ser. All rights reserved.
//

#import "MusicViewController.h"
#import "LMTAFNetwork.h"
#import <UIImageView+WebCache.h>
#import "ReferTableViewCell.h"
#import "PlayModel.h"
#import "SheerScrollView.h"
#import "PlayViewController.h"
@interface MusicViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *referTableView;
@property(nonatomic, retain)NSMutableArray *referDataArr;
@property(nonatomic, retain)NSMutableArray *scrollDataArr;
@property(nonatomic, retain)SheerScrollView *scrollView;


@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"电台";
    
    self.referTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    
    _referTableView.delegate = self;
    _referTableView.dataSource = self;
    [_referTableView registerClass:[ReferTableViewCell class] forCellReuseIdentifier:@"referTVcell"];
    _referTableView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:_referTableView];
    
    [self dataArr];
 
}

- (void)dataArr
{
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=2&contentType=album&device=ios&scale=2&version=4.3.44.2";
    
    [LMTAFNetwork getUrl:urlStr body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        
        
        NSMutableArray *arr = [NSMutableArray arrayWithArray:resposeObject[@"categoryContents"][@"list"]];
        
        [arr removeObjectAtIndex:0];
        
        NSArray *ar = [[arr reverseObjectEnumerator] allObjects];
        NSMutableArray *asd = [NSMutableArray arrayWithArray:ar];
        [asd removeLastObject];
        [asd removeLastObject];
        
        NSArray *zxc = [[asd reverseObjectEnumerator] allObjects];
        
        self.referDataArr = [NSMutableArray arrayWithArray:zxc];
        
        NSLog(@"%@", _referDataArr);
        
        
        NSArray *picArr = resposeObject[@"focusImages"][@"list"];
        //self.scrollDataArr = [PlayModel modelHandlerWithArray:picArr];
        
        self.scrollDataArr = [PlayModel modelHanderWithArray:picArr];
        
        //创建轮播
        [self creatScrollView];
        
        [_referTableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

#pragma mark - tableView协议方法
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _referDataArr.count;
}
//分区名
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _referDataArr[section][@"title"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_referDataArr[section][@"list"] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReferTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"referTVcell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];

    
    [cell.mainImgView sd_setImageWithURL:[NSURL URLWithString:_referDataArr[indexPath.section][@"list"] [indexPath.row][@"albumCoverUrl290"]]];
    
    cell.infoLabel.text = _referDataArr[indexPath.section][@"list"] [indexPath.row][@"intro"];
    cell.countImgView.image = [UIImage imageNamed:@"count"];
    cell.playImgView.image = [UIImage imageNamed:@"play"];
    
    if ([_referDataArr[indexPath.section][@"list"] [indexPath.row][@"playsCounts"] floatValue] > 100000000) {
        cell.backgroundColor = [UIColor clearColor];

        NSString *str = [NSString stringWithFormat:@"%.1f亿", [_referDataArr[indexPath.section][@"list"] [indexPath.row][@"playsCounts"] floatValue] / 100000000];
        cell.numberLabel.text = str;
    }else if ([_referDataArr[indexPath.section][@"list"] [indexPath.row][@"playsCounts"] floatValue] > 10000){
        cell.backgroundColor = [UIColor clearColor];

        NSString *str = [NSString stringWithFormat:@"%.1f万", [_referDataArr[indexPath.section][@"list"] [indexPath.row][@"playsCounts"] floatValue] / 10000];
        cell.numberLabel.text = str;
        
    }else {
        cell.backgroundColor = [UIColor clearColor];

        cell.numberLabel.text = [NSString stringWithFormat:@"%@", _referDataArr[indexPath.section][@"list"][indexPath.row][@"playsCounts"]];
    }
    
    cell.countLabel.text = [NSString stringWithFormat:@"%@集", _referDataArr[indexPath.section][@"list"] [indexPath.row][@"tracks"]];
    
    
    cell.titleLabel.text = _referDataArr[indexPath.section][@"list"] [indexPath.row][@"title"];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
#pragma mark - 轮播图
-(void)creatScrollView{
    self.scrollView = [[SheerScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * PH) animationDuration:6];
    
    NSMutableArray *viewArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < _scrollDataArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * PH)];
        PlayModel *model = _scrollDataArr[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"musicBig"]];
        [viewArray addObject:imageView];
        imageView.userInteractionEnabled = YES;
        //轮播按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, self.view.frame.size.width, 200 * PH);
        //        button.backgroundColor = [UIColor cyanColor];
        [imageView addSubview:button];
        // [button addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 2000 + i;
    }
    [_scrollView setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {
        return [viewArray objectAtIndex:pageIndex];
    }];
    [_scrollView setTotalPagesCount:^NSInteger{
        return viewArray.count;
    }];
    
    _referTableView.tableHeaderView = _scrollView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30 * PH;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *str1 = @"http://mobile.ximalaya.com/mobile/others/ca/album/track/";
    NSString *str2 = @"/true/1/20?position=1&device=ios&title=%E9%83%AD%E5%BE%B7%E7%BA%B2";
    NSString *string = [NSString stringWithFormat:@"%@%@%@", str1, _referDataArr[indexPath.section][@"list"][indexPath.row][@"albumId"], str2];
    
    //NSLog(@"%@", string);
    
    PlayViewController *asd = [[PlayViewController alloc ] init];
    asd.urlStr = string;
    [self.navigationController pushViewController:asd animated:YES];
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"beginPlay" object:_referDataArr[indexPath.section][@"list"] [indexPath.row][@"albumCoverUrl290"]];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue /Users/apple/SnailPost/SnailPost/SnailPost/Video/NewsdestinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
